<template>
  <div class="login_container">
    <div class="login_box">
      <div class="tab-pane">登录</div>
      <!-- 输入框 -->
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        label-width="0px"
      >
        <!-- 输入框1 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 输入框2 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-view"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-row>
            <el-col :span="12">
              <a href="/register">没有账户？点击注册</a>
            </el-col>

            <el-col :span="12">
              <div style="text-align: right">
                <a href="">忘记密码</a>
              </div>
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.status === 400) return this.$message.error('登录失败，请检查账号或者密码！')
        this.$message.success('登录成功')
        // 将登陆用户信息保存到sessionStorage中
        var user = JSON.stringify(res.user)
        sessionStorage.setItem('user', user)
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="less" scoped>
a {
  color: rgb(0, 137, 161);
  text-decoration: none;
}
.login_container {
  height: 100%;
  background-image: url('../../public/images/login_bg.jpg') !important;
  background-size: cover;
}

.tab-pane {
  text-align: center;
  letter-spacing: 12px;
  font-size: 30px;
  color: #02a2d0;
  margin-top: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #c3eafc;
}

.login_box {
  width: 450px;
  height: 350px;
  background-color: rgb(255, 255, 255);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
